import {Component, OnInit} from '@angular/core';
import {ConfigService} from '@kylin/config';
import {NzMessageService} from 'ng-zorro-antd';
@Component({
    selector: 'app-user-avatar',
    templateUrl: './user-avatar.component.html',
    styleUrls: ['./user-avatar.component.less']
})
export class UserAvatarComponent implements OnInit {
    avatar = '';
    itemAvatarUrl = '';
    fileList = [];
    avatarUrl;
    imgHostname = '';

    constructor(private configService: ConfigService,  private message: NzMessageService) {
        this.imgHostname = configService.imgHostname;
    }

    ngOnInit() {
        if (!this.avatar) {
            this.itemAvatarUrl = ''
        } else {
            this.itemAvatarUrl = this.imgHostname + this.avatar
        }
    }

    beforeUpload = (file: File) => {
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
            this.message.create('warning', '上传头像大小不能超过2MB！');
        }
        return isLt2M;
    }

    handleChange(info: { file }): void {
        if (info.file.status === 'uploading') {
            return;
        }
        if (info.file.status === 'done') {
            let urlLength = this.fileList.length - 1;
            this.avatarUrl = this.fileList[urlLength].response.result[0];
            this.itemAvatarUrl = this.imgHostname + this.avatarUrl
        }
    }

}
